package com.fjl.prediction.controller.system;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.fjl.prediction.pojo.SysMenu;
import com.fjl.prediction.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/sys/menu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @GetMapping("/list")
    @PreAuthorize("hasRole('SYS_MENU_LIST')")
    public String list(Model model){
        List<SysMenu> sysMenus = sysMenuService.selectAllMenuList();
        model.addAttribute("list",sysMenus);
        return "system/menu/list";
    }

    @PostMapping("/enable/{id}/{enable}")
    @ResponseBody
    @PreAuthorize("hasRole('SYS_MENU_ENABLE')")
    public boolean enable(@PathVariable String id, @PathVariable String enable){
        UpdateWrapper<SysMenu> wrapper = new UpdateWrapper<>();
        wrapper.set("enable",enable);
        wrapper.eq("id",id);
        return sysMenuService.update(wrapper);
    }

    @GetMapping("/to/add/{pId}/{level}")
    @PreAuthorize("hasRole('SYS_MENU_ADD')")
    public String toAdd(Model model, @PathVariable String pId, @PathVariable String level){
        model.addAttribute("pId",pId);
        model.addAttribute("level",level);
        return "system/menu/add";
    }

    @PostMapping("/insert")
    @ResponseBody
    @PreAuthorize("hasRole('SYS_MENU_ADD')")
    public String insert(String name,String url,String authority,String pId,String level){
        SysMenu sysMenu = new SysMenu();
        sysMenu.setId(IdWorker.getId());
        sysMenu.setName(name);
        sysMenu.setUrl(url);
        if ("#".equals(authority)){
            sysMenu.setAuthority(authority);
        }else{
            sysMenu.setAuthority("ROLE_"+authority);
        }
        sysMenu.setPId(Long.valueOf(pId));
        sysMenu.setLevel(Integer.valueOf(level));
        sysMenu.setEnable(1);
        if (sysMenuService.save(sysMenu)){
            return "true";
        }else{
            return "保存失败";
        }
    }

    @GetMapping("/to/update/{id}")
    @PreAuthorize("hasRole('SYS_MENU_UPDATE')")
    public String toUpdate(Model model, @PathVariable String id){
        SysMenu sysMenu = sysMenuService.getById(id);
        model.addAttribute("sysMenu",sysMenu);
        return "system/menu/edit";
    }

    @PostMapping("/update")
    @ResponseBody
    @PreAuthorize("hasRole('SYS_MENU_UPDATE')")
    public String update(String id,String name,String url,String authority){
        SysMenu sysMenu = new SysMenu();
        sysMenu.setId(Long.valueOf(id));
        sysMenu.setName(name);
        sysMenu.setUrl(url);
        if ("#".equals(authority)){
            sysMenu.setAuthority(authority);
        }else{
            sysMenu.setAuthority("ROLE_"+authority);
        }

        if (sysMenuService.updateById(sysMenu)){
            return "true";
        }else{
            return "保存失败";
        }
    }
}


